$(document).ready(function(){
	$("input").val("");
	$.ajaxSetup({
		beforeSend: function() {
			this.url += (this.url.indexOf("?") == -1? "?":"&") + "rand=" + Math.random();
		},
		complete: function(xhr,status) {
		    if(xhr.getResponseHeader("Connection") == 'close' || 
		    		(xhr.responseJSON && xhr.responseJSON.errorCode == "USER_NO_LOGIN")) {
		      $.messager.alert('提示','<p>会话已失效, 请重新登录！</p>', 'error', function(){
		    	  window.parent.location.href = '/UserLogin.html';
		      });
		    }
		}
	});
	$(".easyui-validatebox").blur(function() {
		$(this).validatebox("validate");
	});
	//邮件保存
	$("#save-btn").click(function (e) {
		e.preventDefault();
		onSave();
	});
	
	//短信保存
	$("#save-btn2").click(function (e) {
		e.preventDefault();
		onSaveSms();
	});
	//邮件数据回显
	$.ajax({
		url:BASE_URL_PARAM_SETTING + '/smtp',
		dataType:'json',
		type:'get',
		success:function(data){
			$("#txtSMTPAddress").val(data.smtpAddress);
			$("#txtPort").val(data.port);
			$("#txtEmailSenderAddress").val(data.emailAddress);
			$("#txtAcount").val(data.userCode);
			$("#txtPassword").val(data.password);
			$('#SaveOrUpdateForm').form('validate');
		},
		error:function(xhr,s,err){
			$.messager.alert('错误',"<p>服务异常，查询邮件服务信息失败，请重新登录或稍后重试！</p>", 'error');
		}
		
		
	});
	//短信数据回显
	$.ajax({
		url:BASE_URL_PARAM_SETTING + '/sms',
		dataType:'json',
		type:'get',
		success:function(data){
			$("#txtSmsAddress").val(data.smsAddress);
			$("#txtSmsPort").val(data.smsPort);
			$("#txtSmsAcount").val(data.userCode);
			$("#txtSmsPassword").val(data.password);
			$('#SaveOrUpdateForm2').form('validate');
		},
		error:function(xhr,s,err){
			$.messager.alert('错误',"<p>服务异常，查询邮件服务信息失败，请重新登录或稍后重试！</p>", 'error');
		}
	});
		 

});
//邮件数据保存
function　onSave(){
	var smtpAddress = $("#txtSMTPAddress").val();
	var port = $("#txtPort").val();
	var emailAddress = $("#txtEmailSenderAddress").val();
	var userCode = $("#txtAcount").val();
	var password = $("#txtPassword").val();
	var jsonData = {
			smtpAddress:smtpAddress,
			port:port,
			emailAddress:emailAddress,
			userCode:userCode,
			password:password
	};
	
	var param = JSON.stringify(jsonData);
	if($('#SaveOrUpdateForm').form('validate')){
		$("#save-btn").attr("disabled", "disabled").text("提交中...");
		$.ajax({
			url:BASE_URL_PARAM_SETTING + '/smtp',
			type:'post',
			contentType:"application/json",
			data: param,
			success:function(){
				$("#save-btn").removeAttr("disabled").text("保存修改");
				$.messager.alert('提示',"<p>修改成功!</p>", 'info',function(){
					//window.location.reload();
				});
			},
			error:function(xhr,s,err){
				$("#save-btn").removeAttr("disabled").text("保存修改");
				$.messager.alert('错误',"<p>服务异常，修改失败，请重新登录或稍后重试！</p>", 'error');
			}
			
			
		});
	}
}
//短信数据保存
function　onSaveSms(){
	var smsAddress = $("#txtSmsAddress").val();
	var smsPort = $("#txtSmsPort").val();
	var userCode = $("#txtSmsAcount").val();
	var password = $("#txtSmsPassword").val();
	var jsonData = {
			smsAddress:smsAddress,
			smsPort:smsPort,
			userCode:userCode,
			password:password
	};
	
	var param = JSON.stringify(jsonData);
	if($('#SaveOrUpdateForm').form('validate')){
		$("#save-btn").attr("disabled", "disabled").text("提交中...");
		$.ajax({
			url:BASE_URL_PARAM_SETTING + '/sms',
			type:'post',
			contentType:"application/json",
			data: param,
			success:function(){
				$("#save-btn").removeAttr("disabled").text("保存修改");
				$.messager.alert('提示',"<p>修改成功!</p>", 'info',function(){
					//window.location.reload();
				});
			},
			error:function(xhr,s,err){
				$("#save-btn").removeAttr("disabled").text("保存修改");
				$.messager.alert('错误',"<p>服务异常，修改失败，请重新登录或稍后重试！</p>", 'error');
			}
			
			
		});
	}
	
	
}

